package com.ly.pj.goods.service;

import com.ly.pj.goods.dao.GoodsDao;
import com.ly.pj.goods.pojo.Goods;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j//lombok插件的功能代替下面注释的日志代码
@Service
public class GoodsServiceImpl implements GoodsService{//has a 强调的是协同
    //private Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);//use a 用到了
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public int updateGoods(Goods goods) {//对于update操作，参数entity中需要一个id值

        return goodsDao.updateGoods(goods);
    }

    @Override
    public Goods findById(Integer id) {
        return goodsDao.findById(id);//将来还可以将查询到的结果在业务逻辑层存储到cache
    }

    @Override
    public int saveGoods(Goods entity) {
        return goodsDao.insertObject(entity);
    }

    @Override
    public int deleteById(Integer id) {
        return goodsDao.deleteById(id);
    }//后续还要在业务进行事务控制，日志记录,....

    @Override
    public List<Goods> findGoods() {
        long startTime=System.currentTimeMillis();
        List<Goods> goodsList=goodsDao.findGoods();
        long endTime=System.currentTimeMillis();
        log.info("findGoods Time --> {}",endTime-startTime);//{}为占位符
        return goodsList;
    }
}
